package com.qq.server.model;

import com.qq.common.Message;
import com.qq.common.MessageType;
import com.qq.server.dao.UserDao;
import com.qq.server.dao.UserDaoImpl;
import com.qq.common.User;
import com.qq.server.tools.JsonUtils;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author weiBin
 * @date 2019/1/10
 */
public class SerConClientThread extends Thread {
    Socket socket;

    public SerConClientThread(Socket socket) {
        this.socket = socket;
    }

    //让该线程去通知其它用户
    public void notifyOther(String iam) {
        //得到所有在线的人的线程
        ConcurrentHashMap hm = ManageClientThread.hm;
        Iterator it = hm.keySet().iterator();

        while (it.hasNext()) {
            Message m = new Message();
            m.setCon(iam);
            m.setMesType(MessageType.message_ret_onLineFriend);
            //取出在线人的id
            String userNo = it.next().toString();
            try {
                ObjectOutputStream oos = new ObjectOutputStream(ManageClientThread.getClientThread(userNo).socket.getOutputStream());
                m.setGetter(userNo);
                oos.writeObject(m);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void run() {
        while (true) {
            //这里该线程就可以接收客户端的信息.
            try {
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message m = (Message) ois.readObject();
                //接受到普通消息
                if (MessageType.message_comm_mes.equals(m.getMesType())) {
                    SerConClientThread sct = ManageClientThread.getClientThread(m.getGetter());
                    //一会完成转发.
                    //取得接收人的通信线程
                    ObjectOutputStream oos = new ObjectOutputStream(sct.socket.getOutputStream());
                    oos.writeObject(m);
                    //接受到好友列表请求
                } else if (MessageType.message_get_onLineFriend.equals(m.getMesType())) {
                    Message m2 = new Message();
                    m2.setMesType(MessageType.message_ret_onLineFriend);
                    UserDao userDao = new UserDaoImpl();
                    List<User> userList = userDao.queryFriendList(m.getSender());
                    List<Map<String, String>> list = new ArrayList<>();
                    for (User user : userList) {
                        Map<String, String> map = new HashMap<>();
                        map.put("id", user.getId());
                        map.put("no", user.getNo());
                        map.put("type", user.getType());
                        map.put("name", user.getName());
                        SerConClientThread clientThread = ManageClientThread.getClientThread(user.getNo());
                        map.put("onLine", clientThread == null ? "0" : "1");
                        list.add(map);
                    }
                    m2.setCon(JsonUtils.objectToJson(list));
                    m2.setGetter(m.getSender());
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(m2);
                }
            } catch (Exception e) {
                e.getMessage();
            }
        }
    }
}
